package com.example.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sms.entity.Administrators;
import com.example.sms.entity.Student;
import com.example.sms.entity.Teacher;
import com.example.sms.entity.dto.LoginDTO;
import com.example.sms.entity.dto.RegisterDTO;
import com.example.sms.exception.R;
import com.example.sms.exception.SimpleException;
import com.example.sms.mapper.AdministratorsMapper;
import com.example.sms.mapper.StudentMapper;
import com.example.sms.response.ResponseModel;
import com.example.sms.service.AdministratorsService;
import com.example.sms.service.StudentService;
import com.example.sms.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Tag(name = "公共接口")
@Slf4j
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdministratorsService AdministratorsService;
    @Operation(summary = "注册")
    @PostMapping("Resister")
    public R Register(@RequestBody RegisterDTO registerDTO){
        switch (registerDTO.getType()) {
            case 0 -> { //添加学生
                QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
                studentQueryWrapper
                        .lambda()
                        .eq(Student::getUsername, registerDTO.getUsername())
                        .eq(Student::getPassword , registerDTO.getPassword());
                if (studentService.getOne(studentQueryWrapper) == null){
                log.info("注册学生 -- " + registerDTO.getName());
                return R.ok(studentService.register(registerDTO));
                }else return R.fail("用户名已存在");
            }
            case 1 -> { //注册教师
                QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
                teacherQueryWrapper
                        .lambda()
                        .eq(Teacher::getUsername, registerDTO.getUsername())
                        .eq(Teacher::getPassword , registerDTO.getPassword());
                if (teacherService.getOne(teacherQueryWrapper) == null){
                log.info("注册教师 -- " + registerDTO.getName());
                return R.ok(teacherService.register(registerDTO));
            }else return R.fail("用户名已存在");
            }
            case 2 -> { //注册管理员
                QueryWrapper<Administrators> administratorsQueryWrapper = new QueryWrapper<>();
                administratorsQueryWrapper
                        .lambda()
                        .eq(Administrators::getUsername, registerDTO.getUsername())
                        .eq(Administrators::getPassword, registerDTO.getPassword());
                if (AdministratorsService.getOne(administratorsQueryWrapper) == null) {
                    log.info("注册管理员 -- " + registerDTO.getName());
                    return R.ok(AdministratorsService.register(registerDTO));
                } else {
                    return R.fail("用户名已存在");
                }
            }
            default -> throw new SimpleException("Unexpected value: " + registerDTO.getType());
        }
    }
    @Operation(summary = "登录")
    @PostMapping("Login")
    public R login(@RequestBody LoginDTO loginDTO){
        switch (loginDTO.getType()) {
            case 0 -> { //学生登录
                log.info("学生登录 -- " + loginDTO.getUsername());
                return R.fail(studentService.login(loginDTO));
            }
            case 1 -> { //教师登录
                log.info("教师登录 -- " + loginDTO.getUsername());
                return R.ok(teacherService.login(loginDTO));
            }
            case 2 -> { //管理员登录
                log.info("管理员登录 -- " + loginDTO.getUsername());
                return R.ok(AdministratorsService.login(loginDTO));
            }
            default -> throw new SimpleException("Unexpected value: " + loginDTO.getType());
        }
    }
}
